package cn.virens.common.util.fastjson;

import cn.virens.common.util.exception.APIException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import org.dromara.hutool.core.array.ArrayUtil;

public class FastJsonConfigExtend extends FastJsonConfig {
    public static final FastJsonConfigExtend INSTANCE = new FastJsonConfigExtend();

    public void addReaderFeatures(JSONReader.Feature... readerFeature) throws APIException {
        this.setReaderFeatures(append(getReaderFeatures(), readerFeature));
    }

    public void addWriterFeatures(JSONWriter.Feature... writerFeature) throws APIException {
        this.setWriterFeatures(append(getWriterFeatures(), writerFeature));
    }

    public void addWriterFilters(Filter... writerFilter) throws APIException {
        this.setWriterFilters(append(getWriterFilters(), writerFilter));
    }

    public void addReaderFilters(Filter... writerFilter) throws APIException {
        this.setReaderFilters(append(getReaderFilters(), writerFilter));
    }

    private static <T> T[] append(T[] array, T[] other) {
        return ArrayUtil.append(array, other);
    }

}
